class Socket{
    constructor(url,https=false){
        this.url = url.replace("http://", "").replace("https://", "")
        this.wsurl = ''
        if (https == true) {
            this.wsurl = 'wss://' + this.url
        }
        else {
            this.wsurl = 'ws://' + this.url
        }
        this.ws = null
        this.env = []
        this.status=false;
        this.init()
        this.Listeners=[]
        this.reconnnet_interval = undefined
        this.reconnnet_time=5
        this.history_msg=[]
    }
    init(){
        try {
            this.ws = new WebSocket(this.wsurl);
            this.ws.onopen = () => {
                this.status = true
                this.findListener('connect')?.cb()
                this.stop_connect_interval()
                this.send_history()

            }
            this.ws.onerror = () => {
                this.status = false
            }
            this.ws.onclose = () => {
                this.status = false
                this.findListener('disconnect')?.cb()
                console.error('当前ws断开连接');
                this.start_connect_interval()
            }
            this.ws.onmessage = (value) => {
                this.message(value)
            }
        } catch (error) {
            this.ws = null
        }
    }
    start_connect_interval() {
        this.stop_connect_interval()
        console.warn(`提示:${this.reconnnet_time}秒后重新连接...`)
        this.reconnnet_interval = setInterval(() => {
            this.init();
        }, this.reconnnet_time * 1000)
    }
    stop_connect_interval() {
        if (this.reconnnet_interval) {
            clearInterval(this.reconnnet_interval);
        }
        this.reconnnet_interval = undefined
    }
    message(value){
        if(value.type=="message"){
            let json_data=JSON.parse(value.data)
            let type = json_data.type
            let cb=this.findListener(type)?.cb
            if(cb){
                cb(json_data.data)
            }
        }
    }
    emit(type,value){
        if (this.status){
            this.send({
                type: type,
                data: value
            })
        }
        else{
            this.history_msg.push({
                type: type,
                data: value
            })
        }
    }
    send(data){
        this.ws.send(JSON.stringify(data));
    }
    send_history() {
        for (let i = 0; i < this.history_msg.length; i++) {
            this.send(this.history_msg[i])            
        }
        this.history_msg=[]
    }
    on(env,cb){
        let env_index = this.findListenerIndex(env)
        if (env_index >=0){
            this.Listeners[env_index].cb=cb
        }
        else{
            this.Listeners.push({
                type:env,
                cb:cb
            })
        }
    }
    off(env){
        let _iListener=this.findListenerIndex(env)
        if(_iListener>=0){
            this._Listeners.splice(_iListener,1)
        }
    }
    findListenerIndex(env){
        return this.Listeners.findIndex((item)=>{
            return item.type==env
        })
    }
    findListener(env){
        let env_index=this.findListenerIndex(env)
        if (env_index >=0){
            return this.Listeners[env_index]
        }
        else{
            return undefined
        }
    }
}
export default Socket;